package com.c.controller;

import com.c.entity.Menu;
import com.c.service.MenuService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:28
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 通过用户名查询菜单
     *
     * @param username
     * @return
     */
    @RequestMapping("/findMenuByUsername")
    public Result findMenuByUsername(String username) {
        try {
            List<Menu> menus = menuService.findMenuByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    /**
     * 查询所有菜单列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> menuList = menuService.list(null);
            return new Result(true, MessageConstant.GETS_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GETS_MENU_FAIL);
        }
    }
    /**
     * 回显角色菜单信息
     *
     * @return
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            Integer[] menuIds = menuService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.FINDBYROLE_MENU_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FINDBYROLE_MENU_FAIL);
        }
    }

    /**
     * 菜单分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findByPage(queryPageBean);
    }

    /**
     * 查询父菜单信息
     *
     * @return
     */
    @RequestMapping("/findAllParent")
    public Result findAllParent() {
        try {
            List<Menu> menus = menuService.findAllParent();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            return menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 编辑 - 数据回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.getById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }


    /**
     * 编辑菜单
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    public Result update(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }
    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            return menuService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
